import React from "react";
import { Table, Switch, message } from 'antd';
import { timeToStr } from '../../../../../utils/time'
import api from "../../../../../api";


const GroupTable = (props: any) => {
    const columns = [
        {
            title: '序号',
            dataIndex: 'number',
            key: 'number',
        },
        {
            title: '图片',
            dataIndex: 'url',
            key: 'url',
        },
        {
            title: '楼盘名称',
            dataIndex: 'building_name',
            key: 'building_name',
        },
        {
            title: '楼盘地址',
            dataIndex: 'building_address',
            key: 'building_address',
        },
        {
            title: '发起人',
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: '起始时间',
            dataIndex: 'create_time',
            key: 'create_time',
            render: (time: any) => {
                return <span>{timeToStr(time)}</span>
            }
        },
        {
            title: '团购状态',
            dataIndex: 'group_state',
            key: 'group_state',
            render: (text: any) => {
                return text ? <span>已开团</span> : <span>未开团</span>
            }
        },
        {
            title: '状态',
            dataIndex: 'state',
            key: 'state',
            render: (state: any, item: any) => {
                return <Switch checkedChildren="正常" unCheckedChildren="异常" checked={state} onChange={() => { stateChange(item) }} />
            }
        },
        {
            title: '操作',
            key: "option",
            render: (text: any, item: any) => {
                return <div className="option-house">
                    <span onClick={() => { del(item.id) }}>删除</span>
                </div>
            }
        },
    ];
    // 方法
    // 状态切换
    const stateChange = (item: any) => {
        api.groupChange({
            id: item.id,
            state: !item.state
        }).then((res: any) => {
            if (res.success) {
                message.success('修改成功');
                // 重新渲染页面
                props.getGroupList();
            } else {
                message.error(res.message);
            }
        })
    };
    // 点击删除
    const del = (id: string) => {
        // 把id传递给父组件的删除方法
        props.delGroup(id)
    }
    return (
        <>
            {props.dataSource[0] && <Table dataSource={props.dataSource} columns={columns} rowKey={record => record.id}
                pagination={false}
            />}
        </>

    )
};

export default GroupTable